import httpx
import parsel


def get_proxy(url, headers):
    with httpx.Client() as client:
        response = client.get(url, headers=headers)
        return response.text


def parse_html(html):
    proxies = []
    selector = parsel.Selector(text=html)
    tr_list = selector.css('.table.table-bordered.table-striped tbody tr')

    for tr in tr_list:
        ip = tr.css('[data-title="IP"]::text').get()
        port = tr.css('[data-title="PORT"]::text').get()
        protocol = tr.css('[data-title="类型"]::text').get().lower()
        proxies.append(f'{protocol}://{ip}:{port}')

    return proxies


def mytest_proxy(proxy_list, headers):
    available_proxies = []
    for proxy in proxy_list:
        proxies = {
            'http://': proxy,
            'https://': proxy,
        }
        print('正在测试...')
        try:
            with httpx.Client(proxies=proxies) as client:
                resp = client.get('http://httpbin.org/ip', headers=headers, timeout=5)
                result = resp.json()
                if result:
                    print('【代理可用】', result)
                    available_proxies.append(proxies)
        except:
            print('【代理不可用】', proxy)

    return available_proxies


def main():
    url = 'https://www.kuaidaili.com/free/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
    }
    html = get_proxy(url, headers)
    proxies = parse_html(html)
    available_proxies = mytest_proxy(proxies, headers)
    print(available_proxies)


if __name__ == '__main__':
    main()
